//
//  KACouponItem.swift
//  kano
//
//  Created by Frank on 2024/11/5.
//

import Foundation
import UIKit

class KACouponItem: UIView {
    
    lazy var var_bgImg: UIImageView = {
        let var_bgImg = UIImageView(frame: CGRectMake(0, 0, width, kscale(72)))
        var_bgImg.image = UIImage(named: "coupon_item")
        return var_bgImg
    }()
    
    lazy var var_tipLabel: UILabel = {
        let var_tipLabel = UILabel()
        var_tipLabel.font = UIFont.systemFont(ofSize: kscale(7), weight: .regular)
        var_tipLabel.textColor = .colorEF6957
        var_tipLabel.backgroundColor = .colorFFD771
        var_tipLabel.textAlignment = .center
        var_tipLabel.text = ""
        var_tipLabel.frame = CGRectMake(0, 0, kscale(58), kscale(13))
        var_tipLabel.numberOfLines = 1
        var_tipLabel.adjustsFontSizeToFitWidth = true
        var_tipLabel.minimumScaleFactor = 0.5
        return var_tipLabel
    }()
    
    lazy var var_moneyLabel: UILabel = {
        let var_moneyLabel = UILabel()
        var_moneyLabel.font = UIFont.systemFont(ofSize: kscale(13), weight: .bold)
        var_moneyLabel.textColor = .white
        var_moneyLabel.textAlignment = .center
        var_moneyLabel.text = ""
        var_moneyLabel.frame = CGRectMake(3, kscale(25), kscale(60), kscale(15))
        var_moneyLabel.numberOfLines = 1
        var_moneyLabel.adjustsFontSizeToFitWidth = true
        var_moneyLabel.minimumScaleFactor = 0.5
        return var_moneyLabel
    }()
    
    lazy var var_typeLabel: UILabel = {
        let var_typeLabel = UILabel()
        var_typeLabel.font = UIFont.systemFont(ofSize: kscale(7), weight: .regular)
        var_typeLabel.textColor = .white
        var_typeLabel.textAlignment = .center
        var_typeLabel.numberOfLines = 1
        var_typeLabel.adjustsFontSizeToFitWidth = true
        var_typeLabel.minimumScaleFactor = 0.5
        var_typeLabel.text = ""
        var_typeLabel.frame = CGRectMake(3, kscale(42), kscale(60), kscale(10))
        return var_typeLabel
    }()
    
    lazy var var_useableLabel: UILabel = {
        let var_useableLabel = UILabel()
        var_useableLabel.font = UIFont.systemFont(ofSize: kscale(10), weight: .regular)
        var_useableLabel.textColor = .colorFF5953
        var_useableLabel.textAlignment = .left
        var_useableLabel.numberOfLines = 2
        var_useableLabel.text = ""
        var_useableLabel.frame = CGRectMake(kscale(74), kscale(7), width - kscale(84), kscale(26))
        return var_useableLabel
    }()
    
    lazy var var_dateLabel: UILabel = {
        let var_dateLabel = UILabel()
        var_dateLabel.font = UIFont.systemFont(ofSize: kscale(8), weight: .regular)
        var_dateLabel.textColor = .colorA19389
        var_dateLabel.textAlignment = .left
        var_dateLabel.numberOfLines = 1
        var_dateLabel.text = "Valid until:"
        var_dateLabel.frame = CGRectMake(kscale(74), var_useableLabel.bottom, width - kscale(84), kscale(13))
        return var_dateLabel
    }()
    
    lazy var var_maxLimitLabel: UILabel = {
        let var_maxLimitLabel = UILabel()
        var_maxLimitLabel.font = UIFont.systemFont(ofSize: kscale(8), weight: .regular)
        var_maxLimitLabel.textColor = .colorA19389
        var_maxLimitLabel.textAlignment = .left
        var_maxLimitLabel.numberOfLines = 0
        var_maxLimitLabel.text = "Maximum discount of "
//        var_maxLimitLabel.frame = CGRectMake(kscale(74), var_dateLabel.bottom, width - kscale(102), kscale(13))
        return var_maxLimitLabel
    }()
    
    lazy var var_useBtn: UIButton = {
        let var_useBtn = UIButton(frame: CGRectMake(kscale(174), kscale(48), kscale(52), kscale(18)))
        var_useBtn.setTitle("Use Now", for: .normal)
        var_useBtn.setTitleColor(.white, for: .normal)
        var_useBtn.titleLabel?.font = UIFont.systemFont(ofSize: kscale(9), weight: .regular)
        var_useBtn.backgroundColor = .colorFF485E
        var_useBtn.layer.cornerRadius = kscale(9)
        var_useBtn.layer.masksToBounds = true
        var_useBtn.addTarget(self, action: #selector(ka_useBtnClick(_:)), for: .touchUpInside)
        return var_useBtn
    }()
    
    
    var handler:((KAHomeCoupon)->())? = nil

    @objc func ka_useBtnClick(_ sender: UIButton) {
        sender.preventDoubleTap()
        if handler != nil {
            handler!(var_model!)
        }
    }
    
    var var_model: KAHomeCoupon? {
        didSet {
            guard let model = var_model else { return }
            if model.voucherTransactionMethod == 1 {
                var_tipLabel.isHidden = false
                var_tipLabel.text = "Installment"
            } else if model.voucherTransactionMethod == 4 {
                var_tipLabel.isHidden = false
                var_tipLabel.text = "Universal"
            } else {
                var_tipLabel.isHidden = true
            }
            
            if model.popupNotification == 2 {
                let percent = (Decimal(1)-Decimal(model.voucherMonetaryValue))*100
                var_moneyLabel.text = "\(percent)%"
            } else if model.popupNotification == 4 {
                var_moneyLabel.text = "Interest-Free"
            } else {
                var_moneyLabel.text = "₦\(Int(model.voucherMonetaryValue))"
            }
            
            if model.popupNotification == 2 {
                var_typeLabel.text = "Discount Coupon"
            } else if model.popupNotification == 4 {
                var_typeLabel.text = "Interest-Free"
            } else {
                var_typeLabel.text = "Cash Voucher"
            }
            
            var_useableLabel.text = "Loan amount is available within \(model.eligibilityThreshold)-\(model.conditionMaximumBoundary)"
            var_dateLabel.text = "Valid until:\(Date.ka_stringWith(timestamp: model.withdrawalAccountDetails, format: "dd-MM-yyyy"))"
            
            if model.feedbackTimestamp != 0 {
                var_maxLimitLabel.isHidden = false
                var_maxLimitLabel.text = "Maximum discount of \(model.feedbackTimestamp)"
                var_maxLimitLabel.snp.makeConstraints { make in
                    make.left.equalTo(kscale(74))
                    make.top.equalTo(var_dateLabel.snp.bottom)
                    make.right.equalTo(var_useBtn.snp.left).offset(-6)
                }
            } else {
                var_maxLimitLabel.isHidden = true
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        ka_setUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func ka_setUI() {
        backgroundColor = .colorF4F4FD
        addSubview(var_bgImg)
        addSubview(var_tipLabel)
        var_tipLabel.ka_addTopLeftAndRightBottomCorner(withRadius: kscale(4))
        addSubview(var_moneyLabel)
        addSubview(var_typeLabel)
        
        addSubview(var_useableLabel)
        addSubview(var_dateLabel)
        addSubview(var_maxLimitLabel)
        addSubview(var_useBtn)
    }
}
